package com.xxgc.cloud.error.center.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 公共的返回类
 */
@Data
@ApiModel
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
@Slf4j
public class ErrResult<T> implements Serializable {

    @ApiModelProperty(value = "请求状态码")
    private int code;

    @ApiModelProperty(value = "响应信息")
    private String msg;

    @ApiModelProperty(value = "当前系统版本")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String version;

    @ApiModelProperty(value = "当前环境")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String environment;

    //属性为 空（""） 或者为 NULL 都不序列化
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty(value = "数据")
    private T data;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty(value = "异常描述")
    private String exceptionDescription;

    public ErrResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ErrResult(int code, String msg, String ed) {
        this.code = code;
        this.msg = msg;
        this.exceptionDescription = ed;
    }

    public ErrResult(int code, String msg, String ed, T data) {
        this.code = code;
        this.msg = msg;
        this.exceptionDescription = ed;
        this.data = data;
    }
    public static <T> ErrResult<T> success(String msg,String version) {
        return new ErrResult<>(200, msg, version);
    }
    public static <T> ErrResult<T> error(String error, String ed) {
        return new ErrResult<>(500, error, ed);
    }
    public static <T> ErrResult<T> error(String error, String ed, T data) {
        return new ErrResult(500, error, ed, data);
    }
    /**--------------当公司还有其他业务状态返回可以在以下添加---------------*/
}